<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'updatePerson.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
</head>

<body>
	<table>
		<tr>
			<td>电话号码</td>
			<td><input type="text" id="number"></td>
		</tr>
		<tr>
			<td>姓名</td>
			<td><input type="text" id="name"></td>
		</tr>
		<tr>
			<td>性别</td>
			<td><input type="text" id="sex"></td>
		</tr>
		<tr>
			<td>住址</td>
			<td><input type="text" id="address"></td>
		</tr>
		<tr>
			<td>e-mail</td>
			<td><input type="text" id="email"></td>
		</tr>
		<tr>
			<td colspan="2" align="right"><button id="updatePerson">添加该条记录</button></td>
		</tr>
	</table>
</body>
</html>
<script>
	$(document).ready(function() {
		var url = location.search;
		var str;
		if (url.indexOf("?") != -1) {
			str = url.substring(url.lastIndexOf("?", 0) + 4, url.length);
		}
		//alert(str);
		$.ajax({
			type : "GET",
			contentType : "applicaiton/json",
			dataType : "Json",
			url : "person/" + str,
			success : function(data) {
				if (data.code == 200)
					showData(data);
			},
			error : function() {
				alert("服务器开小差了");
			}
		});

	});
	function showData(data) {
		if (data.person.number != null) {
			$("#number").attr("value", data.person.number);
		} else {
			$("#number").attr("value", "");
		}
		if (data.person.name != null) {
			$("#name").attr("value", data.person.name);
		} else {
			$("#name").attr("value", "");
		}
		if (data.person.sex != null) {
			$("#sex").attr("value", data.person.sex);
		} else {
			$("#sex").attr("value", "");
		}
		if (data.person.address != null) {
			$("#address").attr("value", data.person.address);
		} else {
			$("#address").attr("value", "");
		}
		if (data.person.email != null) {
			$("#email").attr("value", data.person.email);
		} else {
			$("#email").attr("value", "");
		}

	}

	$("#updatePerson").click(function() {
		//alert($("#name").val());
		var url = location.search;
		var id;
		if (url.indexOf("?") != -1) {
			id = url.substring(url.lastIndexOf("?", 0) + 4, url.length);
		}
		var number=$("#number").val();
		var name = $("#name").val();
		var sex = $("#sex").val();
		var address = $("#address").val();
		var email = $("#email").val();
		var person = {};
		person["id"]=id;
		person["number"] = number;
		person["name"] = name;
		person["sex"] = sex;
		person["address"] = address;
		person["email"] = email;
		alert(JSON.stringify(person));
		$.ajax({
			type : "PUT",
			contentType : "application/json",
			dataType : "Json",
			data : JSON.stringify(person),
			url : "updatePerson",
			success : function(data) {
				if (data.code == 500)
					alert("您输入的电话号码已存在");
				if (data.code == 200) {
					alert("修改成功");
					window.location.href = "jsp/index.jsp";
				}
			},
			error : function() {
				alert("服务器开小差了");
			}
		});

	});
</script>